package ca

import (
	"errors"
)

// -H, --home string              Client's home directory (default "/home/francis/fabric-ca/client-config")
// --tls.certfiles strings        A list of comma-separated PEM-encoded trusted certificate files (e.g. root1.pem,root2.pem)
// -u, --url string                   URL of fabric-ca-server (default "http://localhost:7054")

// export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/org0/ca/crypto/ca-cert.pem
// export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/org0/ca/admin
// fabric-ca-client enroll -d -u https://admin:adminpw@0.0.0.0:7054
func (*FabricCaCooridnator) enrollAdmin(tlsCert, fccHome, url string) error {
	if url == "" {
		return errors.New("URL of fabric-ca-server is empty")
	}
	//if tlsCert != "" {
	//	os.Setenv("FABRIC_CA_CLIENT_TLS_CERTFILES", tlsCert)
	//}
	//if fccHome != "" {
	//	os.Setenv("FABRIC_CA_CLIENT_HOME", fccHome)
	//}

	return runMain([]string{"fabric-ca-client", "enroll", "-d", "-u", url, "--home", fccHome, "--tls.certfiles", tlsCert})
}

func (*FabricCaCooridnator) enroll(flags Flags) error {

	frontCmd := []string{"fabric-ca-client", "enroll", "-d"}
	fullCmd := flags.follow(frontCmd)

	return runMain(fullCmd)
}
